const { Controller } = require('egg');

class searchController extends Controller {
  async searchFriend() {
    // 搜索好友
    const { ctx, app } = this;
    // 1. 参数验证
    ctx.validate({
      keyword: {
        type: 'string',
        required: true,
        desc: '关键字'
      }
    });
    // 2. 获取参数
    const { keyword } = ctx.query;
    const id = ctx.authUser.id;
    console.log(keyword, id);
    // 3. 数据库搜索用户  做模糊查询 关联用户表
    const result = await app.model.Friend.findAll({
      where: {
        [app.Sequelize.Op.or]: [
          {
            user_id: id,
            nickname: {
              [app.Sequelize.Op.like]: `%${keyword}%`
            }
          }
        ]
      },
      include: [
        {
          model: app.model.User,
          as: 'friendInfo',
          attributes: ['username', 'avatar']
        }
      ],
      attributes: {}
    });
    // 4. 返回结果
    ctx.success(result);
  }
  async searchUser() {
    // 搜索用户
    const { ctx, app } = this;
    // 1. 参数验证
    ctx.validate({
      keyword: {
        type: 'string',
        required: true,
        desc: '关键字'
      }
    });
    const keyword = ctx.query.keyword;
    const id = ctx.authUser.id;
    // 2. 数据库搜索用户  做模糊查询
    const result = await app.model.User.findAll({
      where: {
        [app.Sequelize.Op.and]: [
          {
            username: {
              [app.Sequelize.Op.like]: `%${keyword}%`
            },
            id: {
              [app.Sequelize.Op.ne]: id
            }
          }
        ]
      },
      attributes: {
        exclude: ['password'] // 排除密码字段
      }
    });
    // 3. 找到Friend中user_id是自己的
    const friendIds = await app.model.Friend.findAll({
      where: {
        user_id: id
      }
    });
    const friendIdList = friendIds.map((item) => item.friend_id);
    // 4. 将friendIdList中friend_id等于result中id的 删除
    result.forEach((item) => {
      if (friendIdList.includes(item.id)) {
        result.splice(result.indexOf(item), 1);
      }
    });

    ctx.success(result);
  }
  async searchUserPc() {
    // 搜索用户
    const { ctx, app } = this;
    // 1. 参数验证
    ctx.validate({
      keyword: {
        type: 'string',
        required: true,
        desc: '关键字'
      }
    });
    const keyword = ctx.query.keyword;
    const id = ctx.authUser.id;
    // 2. 数据库搜索用户  做模糊查询
    let result = await app.model.User.findAll({
      where: {
        [app.Sequelize.Op.and]: [
          {
            username: {
              [app.Sequelize.Op.like]: `%${keyword}%`
            },
            id: {
              [app.Sequelize.Op.ne]: id
            }
          }
        ]
      },
      attributes: {
        exclude: ['password'] // 排除密码字段
      }
    });
    // 3. 找到Friend中user_id是自己的
    const friendIds = await app.model.Friend.findAll({
      where: {
        user_id: id
      }
    });
    const friendIdList = friendIds.map((item) => item.friend_id);
    // 4. 将friendIdList中friend_id等于result中id的 删除
    result = JSON.parse(JSON.stringify(result));
    result.forEach((item) => {
      if (friendIdList.includes(item.id)) {
        item.isFriend = true;
      }
    });

    ctx.success(result);
  }
}
module.exports = searchController;
